package cn.dong.edu.service.impl;

import cn.dong.edu.mapper.TeacherMapper;
import cn.dong.edu.service.TeacherService;
import cn.dong.edu.domain.Teacher;
import cn.dong.edu.domain.query.QueryTeacher;
import cn.dong.base.handler.CustomException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author You-Dong
 * @since 2021-01-30
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {


    /**
     * 分页组合查询
     *
     * @param current
     * @param size
     * @param queryTeacher
     * @return
     */
    @Override
    public IPage<Teacher> findByCondition(Integer current, Integer size, QueryTeacher queryTeacher) {
        if(current <= 0 || size <= 0){
            throw new CustomException(400, "错误，分页参数列表不正确");
        }
        // 开启分页
        Page<Teacher> page = new Page<Teacher>(current, size);
        // 创建条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper<Teacher>();
        // 判断
        if (queryTeacher == null) {
            IPage<Teacher> teacherIPage = baseMapper.selectPage(page, null);
            return teacherIPage;
        }
        if (!StringUtils.isEmpty(queryTeacher.getName())) {
            wrapper.like("name", queryTeacher.getName());
        }
        if (!StringUtils.isEmpty(queryTeacher.getLevel())) {
            wrapper.eq("level", queryTeacher.getLevel());
        }
        if (!StringUtils.isEmpty(queryTeacher.getBegin())) {
            wrapper.ge("gmt_create", queryTeacher.getBegin());
        }
        if (!StringUtils.isEmpty(queryTeacher.getEnd())) {
            wrapper.le("gmt_create", queryTeacher.getEnd());
        }
        // 根据分查询
        return super.baseMapper.selectPage(page, wrapper);
    }

    /**
     * 查询前4名热度讲师
     *
     * @return
     */
    @Override
    @Cacheable(value = "teacher", key = "'getHotTeacher'")
    public List<Teacher> getHotTeacher() {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<Teacher>();
        wrapper.eq("level", 2);
        wrapper.orderByAsc("sort");

        wrapper.last("limit 4");
        return baseMapper.selectList(wrapper);}
}
